package com.lineage.data.npc.quest;

import com.lineage.data.QuestClass;
import com.lineage.data.cmd.CreateNewItem;
import com.lineage.data.executor.NpcExecutor;
import com.lineage.data.quest.EWLv40_1;
import com.lineage.server.datatables.NpcTable;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_CloseList;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.types.Point;
import com.lineage.server.world.World;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Npc_Roi extends NpcExecutor {
  private static final Log _log = LogFactory.getLog(Npc_Roi.class);
  
  public static NpcExecutor get() {
    return new Npc_Roi();
  }
  
  public int type() {
    return 7;
  }
  
  public void talk(L1PcInstance pc, L1NpcInstance npc) {
    try {
      if (npc.getMaster() != null) {
        if (npc.getMaster().equals(pc)) {
          pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "roi2"));
        } else {
          pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "roi"));
        } 
        return;
      } 
      if (pc.getQuest().isEnd(EWLv40_1.QUEST.get_id())) {
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "roi"));
        return;
      } 
      if (pc.getLevel() >= EWLv40_1.QUEST.get_questlevel()) {
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "roi1"));
      } else {
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "roi"));
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void action(L1PcInstance pc, L1NpcInstance npc, String cmd, long amount) {
    boolean isCloseList = false;
    if (npc.getMaster() != null)
      return; 
    if (pc.getQuest().isEnd(EWLv40_1.QUEST.get_id())) {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "roi"));
      return;
    } 
    if (pc.getLevel() >= EWLv40_1.QUEST.get_questlevel()) {
      if (cmd.equalsIgnoreCase("start")) {
        L1Npc l1npc = NpcTable.get().getTemplate(70957);
        QuestClass.get().startQuest(pc, EWLv40_1.QUEST.get_id());
        isCloseList = true;
      } 
    } else {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "roi"));
    } 
    if (isCloseList)
      pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId())); 
  }
  
  public void attack(L1PcInstance pc, L1NpcInstance npc) {
    try {
      if (npc.getNpcId() != 70957)
        return; 
      if (pc == null)
        return; 
      Iterator<L1Object> iterator = World.get().getVisibleObjects((L1Object)npc).iterator();
      while (iterator.hasNext()) {
        L1Object object = iterator.next();
        if (object instanceof L1NpcInstance) {
          L1NpcInstance tgnpc = (L1NpcInstance)object;
          if (tgnpc.getNpcTemplate().get_npcId() != 70964 || 
            npc.getLocation().getTileLineDistance((Point)pc.getLocation()) >= 3 || 
            tgnpc.getLocation().getTileLineDistance((Point)pc.getLocation()) >= 3)
            continue; 
          pc.getQuest().set_end(EWLv40_1.QUEST.get_id());
          CreateNewItem.getQuestItem((L1Character)pc, npc, 41003, 1L);
          npc.setParalyzed(true);
          npc.deleteMe();
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
